﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.DataViz.NetworkDiagram.NetworkData.ExternalOrgs
{
    /// <summary>
    /// 
    /// </summary>
    public class ExternalOrgDistanceCalculator : ILinkDistanceCalculator
    {
        private string _OrgIDField;
        private string _OrgNameField;
        private string _EinField;
        private string _DunsIDField;

        #region Implementation of ILinkDistanceCalculator

        /// <summary>
        /// 
        /// </summary>
        public Dictionary<string, SimpleDataType> RequiredArguments
        {
            get
            {
                Dictionary<string,SimpleDataType> reqArgs=new Dictionary<string, SimpleDataType>();
                reqArgs.Add("OrgID",SimpleDataType.IntNumberType);
                reqArgs.Add("OrgName", SimpleDataType.TextType);
                reqArgs.Add("EIN", SimpleDataType.TextType);
                reqArgs.Add("DunsID", SimpleDataType.TextType);

                return reqArgs;
            }
        }

        /// <summary>
        /// setup field -> arg mappings
        /// </summary>
        /// <param name="mappings"></param>
        public void InitFieldMappings(Dictionary<string, string> mappings)
        {
            if (mappings != null && mappings.Count > 0)
            {
                foreach (string key in mappings.Keys)
                {
                    if (mappings[key] == "OrgID")
                        this._OrgIDField = key;
                    else if (mappings[key] == "OrgName")
                        this._OrgNameField = key;
                    else if (mappings[key] == "EIN")
                        this._EinField = key;
                    else if (mappings[key] == "DunsID")
                        this._DunsIDField = key;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dr1"></param>
        /// <param name="dr2"></param>
        /// <returns></returns>
        public double? CalculateLinkDistance(DataRow dr1, DataRow dr2)
        {
            if(!string.IsNullOrEmpty(this._OrgIDField))
            {
                if(dr1[this._OrgIDField]!=null && dr2[this._OrgIDField]!=null && 
                    dr1[this._OrgIDField]==dr2[this._OrgIDField])
                {
                    return 0;
                }
            }
            if (!string.IsNullOrEmpty(this._EinField))
            {
                if (dr1[this._EinField] != null && dr2[this._EinField] != null &&
                    dr1[this._EinField] == dr2[this._EinField])
                {
                    return 0;
                }
            }
            if (!string.IsNullOrEmpty(this._DunsIDField))
            {
                if (dr1[this._DunsIDField] != null && dr2[this._DunsIDField] != null &&
                    dr1[this._DunsIDField] == dr2[this._DunsIDField])
                {
                    return 0;
                }
            }
            if (!string.IsNullOrEmpty(this._OrgNameField))
            {
                if (dr1[this._OrgNameField] != null && dr2[this._OrgNameField] != null &&
                    dr1[this._OrgNameField] == dr2[this._OrgNameField])
                {
                    return 0.1;
                }
            }

            double? notDefined = null;
            return notDefined;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity1"></param>
        /// <param name="entity2"></param>
        /// <returns></returns>
        public double? CalculateLinkDistance(BaseEntity entity1, BaseEntity entity2)
        {
            if (!string.IsNullOrEmpty(this._OrgIDField))
            {
                if (entity1.GetPropertyValue(this._OrgIDField) != null && 
                    entity2.GetPropertyValue(this._OrgIDField) != null &&
                    entity1.GetPropertyValue(this._OrgIDField) == entity2.GetPropertyValue(this._OrgIDField))
                {
                    return 0;
                }
            }
            if (!string.IsNullOrEmpty(this._EinField))
            {
                if (entity1.GetPropertyValue(this._EinField) != null && entity2.GetPropertyValue(this._EinField) != null &&
                    entity1.GetPropertyValue(this._EinField) == entity2.GetPropertyValue(this._EinField))
                {
                    return 0;
                }
            }
            if (!string.IsNullOrEmpty(this._DunsIDField))
            {
                if (entity1.GetPropertyValue(this._DunsIDField) != null && entity2.GetPropertyValue(this._DunsIDField) != null &&
                    entity1.GetPropertyValue(this._DunsIDField) == entity2.GetPropertyValue(this._DunsIDField))
                {
                    return 0;
                }
            }
            if (!string.IsNullOrEmpty(this._OrgNameField))
            {
                if (entity1.GetPropertyValue(this._OrgNameField) != null && entity2.GetPropertyValue(this._OrgNameField) != null &&
                    entity1.GetPropertyValue(this._OrgNameField) == entity2.GetPropertyValue(this._OrgNameField))
                {
                    return 0.1;
                }
            }

            double? notDefined = null;
            return notDefined;
        }

        #endregion
    }
}
